﻿using System;
using IQToolkit;
using IQToolkit.Data;
using Infrastucture.Factories;
using TimeSplicerModule.DataModels;
using TimeSplicerModule.DomainModels;


namespace TimeSplicerModule.Factories
{
    public interface ITaskScheduleFactory : Infrastucture.Factories.IFactory<TaskScheduleDataModel, TaskScheduleModel>
    { }

    public class TaskScheduleFactory : Factory<TaskScheduleDataModel, TaskScheduleModel>, ITaskScheduleFactory
    {
        public TaskScheduleFactory(DbEntityProvider provider, IEntityTable<TaskScheduleDataModel> db)
            : base(provider, db)
        {

        }

        public override TaskScheduleModel Add(TaskScheduleModel obj)
        {
            // Need to check for exsting task record
            var existing = this.Get(x => x.TaskGuid == obj.TaskGuid);
            

            if (existing == null)
                return base.Add(obj);
            else
            {
                obj.ScheduleGuid = existing.ScheduleGuid;
                return this.Save(obj);
            }
        }
    }
}
